package com.tul.exercise.base.thread.base;


import java.util.concurrent.TimeUnit;

/**
 * 通过内部类将线程代码隐藏仔类中
 */
public class ThreadVariations {
    public static void main(String[] args) {
        new InnerThread1("InnerThread1");
        new InnerThread2("InnerThread2");
        new InnerRunnable1("InnerRunnable1");
        new InnerRunnable2("InnerRunnable2");

        new ThreadMethod("ThreadMethod").runTask();
    }
}

/**
 * 继承Thread的内部类
 */
class InnerThread1 {
    private int countDown = 5;
    /**
     * 内部类线程
     */
    private Inner inner;

    // 构造函数
    public InnerThread1(String name) {
        inner = new Inner(name);
    }

    /**
     * 内部类
     */
    private class Inner extends Thread {

        /**
         * 构造函数中启动线程
         *
         * @param name 线程名称
         */
        Inner(String name) {
            super(name);
            start();
        }

        public String toString() {
            return getName() + ": " + countDown;
        }

        /**
         * 任务
         */
        public void run() {

            try {
                while (true) {
                    System.out.println(this);
                    if (--countDown == 0) {
                        return;
                    }
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (InterruptedException e) {
                System.out.println(e);
            }

        }

    }

}

/**
 * 构造函数中使用匿名内部类
 */
class InnerThread2 {
    private int countDown = 5;
    private Thread t;

    public InnerThread2(String name) {

        // 匿名内部类
        t = new Thread(name) {
            public String toString() {
                return getName() + ": " + countDown;
            }

            /**
             * 任务
             */
            public void run() {

                try {
                    while (true) {
                        System.out.println(this);
                        if (--countDown == 0) {
                            return;
                        }
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (InterruptedException e) {
                    System.out.println(e);
                }

            }
        };

        // 启动线程
        t.start();
    }

}

/**
 * Runnable 内部类
 */
class InnerRunnable1 {
    private int countDown = 5;
    // 内部类成员
    private Inner inner;

    /**
     * 内部类
     */
    private class Inner implements Runnable {
        // 线程成员
        Thread t;

        // 构造函数启动线程
        Inner(String name) {
            t = new Thread(name);
            t.start();
        }

        @Override
        public void run() {
            try {
                while (true) {
                    System.out.println(this);
                    if (--countDown == 0) {
                        return;
                    }
                    TimeUnit.SECONDS.sleep(2);
                }

            } catch (InterruptedException e) {
                System.out.println(e);
            }

        }

        public String toString() {
            return t.getName() + "： " + countDown;
        }
    }

    public InnerRunnable1(String name) {
        inner = new Inner(name);
    }
}

/**
 * 构造函数中使用匿名内部类
 */
class InnerRunnable2 {
    private int countDown = 5;

    private Thread t;

    public InnerRunnable2(String name) {
        t = new Thread(name) {
            public void run() {
                try {
                    while (true) {
                        System.out.println(this);
                        if (--countDown == 0) {
                            return;
                        }
                        TimeUnit.SECONDS.sleep(3);
                    }

                } catch (InterruptedException e) {
                    System.out.println("中断异常" + e.fillInStackTrace());
                }
            }

            public String toString() {
                return getName() + ": " + countDown;
            }
        };

        t.start();
    }
}

class ThreadMethod{
    private int countDown = 5;

    private Thread t;

    private String name;

    public ThreadMethod(String name) {
        this.name = name;
    }

    public void runTask() {
        if (t == null) {
            t = new Thread(name) {
                public void run() {
                    try {
                        while (true) {
                            System.out.println(this);
                            if (--countDown == 0) {
                                return;
                            }
                            TimeUnit.SECONDS.sleep(3);
                        }

                    } catch (InterruptedException e) {
                        System.out.println("中断异常" + e.fillInStackTrace());
                    }
                }

                public String toString() {
                    return getName() + ": " + countDown;
                }
            };
            t.start();
        }
    }
}
